package com.xiangyueEducation.uploaderCloud.controller;


import com.xiangyueEducation.uploaderCloud.utils.JwtHelper;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.pojo.entity.User;
import com.xiangyueEducation.uploaderCloud.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("account")
public class AccountController {

    @Autowired
    private LoginService loginService;
    @Autowired
    JwtHelper jwtHelper;


    // 登录
    @PostMapping("login")
    public Result<?> login(@RequestBody User user){
        return loginService.login(user);
    }


    /**
     * 检查用户名是否被使用
     * @param username
     */
    @GetMapping("check/{username}")
    public Result<?> checkUsernameUsed(@PathVariable String username){
        return loginService.checkUsernameUsed(username);
    }

    /**
     * 获取验证码
     * @param number
     * @return
     */
    @GetMapping("register/captcha")
    public Result<?> getCaptcha(@RequestParam String number,@RequestParam String email){
        return loginService.getCaptcha(number,email);
    }

    /**
     * 验证验证码
     * @param captcha
     * @return
     */
    @PostMapping("register/captcha/verify")
    public Result<?> verifyCaptcha(String username,String captcha){
        return loginService.verifyCaptcha(username,captcha);
    }


    /**
     * 注册功能
     * @param registerInfo
     */
    @PostMapping("register")
    public Result<?> register(@RequestBody User registerInfo,@RequestParam String captcha){
        return loginService.register(registerInfo,captcha);
    }

    @PostMapping("add")
    public Result<?> add(@RequestBody User registerInfo){
        return loginService.add(registerInfo);
    }


    /**
     * 检查登录状态
     * @param token
     */
    @GetMapping("check/login/status")
    public Result<?> checkLogin(@RequestHeader String token){
        return loginService.checkLogin(token);
    }


    /**
     * 修改密码
     * @param pwdInfo
     * @param token
     * @return
     */
    @PostMapping("change/pwd")
    public Result<?> changePwd(@RequestBody HashMap<String,String> pwdInfo,
                            @RequestHeader String token){
        return loginService.changePwd(pwdInfo,token);
    }



}
